import requests
import json
import os
from bs4 import BeautifulSoup
from beeize.scraper import Scraper

scraper = Scraper()

# 从环境变量读取参数
oid = os.getenv('OID', '1955931573')
type_ = os.getenv('TYPE', '1')
mode = os.getenv('MODE', '3')
pagination_str = os.getenv('PAGINATION_STR', '{"offset":""}')
plat = os.getenv('PLAT', '1')
web_location = os.getenv('WEB_LOCATION', '1315875')
w_rid = os.getenv('W_RID', 'd825c5108a70700cfba6ee87987e8c1e')
wts = os.getenv('WTS', '1719290178')

url = "https://api.bilibili.com/x/v2/reply/wbi/main?oid=1605782142&type=1&mode=3&pagination_str=%7B%22offset%22:%22%7B%5C%22type%5C%22:1,%5C%22direction%5C%22:1,%5C%22session_id%5C%22:%5C%221760554195861117%5C%22,%5C%22data%5C%22:%7B%7D%7D%22%7D&plat=1&web_location=1315875&w_rid=be6c014ebcd9f20cab8cf8fda61cb7de&wts=1719291281"


headers = {
    'authority': 'api.bilibili.com',
    'method': 'GET',
    'path': '/x/v2/reply/wbi/main?oid=1955931573&type=1&mode=3&pagination_str=%7B%22offset%22:%22%22%7D&plat=1&seek_rpid=&web_location=1315875&w_rid=d825c5108a70700cfba6ee87987e8c1e&wts=1719290178',
    'scheme': 'https',
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br, zstd',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,ko;q=0.5',
    'Cookie': 'buvid3=D0C60F90-DD98-581D-9982-F3CC82DFB32994919infoc; b_nut=1704627794; _uuid=109891817-6588-235A-BF6E-84FB83A6626595705infoc; buvid4=4655220D-3E2A-D99B-309C-597A9F16D6AF33007-023112911-; buvid_fp=7d93d2b59a7f4a297810a8e0aca80997; rpdid=|(J|~m|R||um0J\'u~|RYk~~m~; b_lsid=104E61010ED_1904DABA3F4; bsource=search_bing; enable_web_push=DISABLE; header_theme_version=CLOSE; home_feed_column=5; browser_resolution=1872-924; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTk1NDkyMjIsImlhdCI6MTcxOTI4OTk2MiwicGx0IjotMX0._Z_TyDeGgsFptinePoo4tuXF2_igGLP0afQPkG2hgLk; bili_ticket_expires=1719549162; SESSDATA=239e72e4%2C1734842047%2C88e08%2A62CjBiRASROoU0r8LeaqRF02X_XprRAhKp_H7hQlPpMSlUgeI1_zRAAMNW0MhxnomYxVESVmwtanR3RmtrcXhzTFp2aUVXbk54WWdjVkdRM3g4US1sNFNJemp4U3c1Q29KQWZxcWtycTd1dHlaY3hBa28zZ3ZraS1CM1ZkNEtvWlpRTUZKc2dVSnNnIIEC; bili_jct=ad44df75745897e1e060c1b94c28ec12; DedeUserID=128976142; DedeUserID__ckMd5=e09cecb1f7d50307; sid=7265tir6; CURRENT_FNVAL=16',
    'Origin': 'https://www.bilibili.com',
    'Priority': 'u=1, i',
    'Referer': 'https://www.bilibili.com/video/BV1ry411q7Ls/?spm_id_from=333.999.0.0&vd_source=918fd73dbfa09c7ec089547ba774def1',
    'Sec-Ch-Ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Microsoft Edge";v="126"',
    'Sec-Ch-Ua-Mobile': '?0',
    'Sec-Ch-Ua-Platform': '"Windows"',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0'
}

response = requests.get(url, headers=headers)

if response.status_code == 200:
    data = response.json()
    if data['code'] == 0:
        replies = data['data']['replies']
        for reply in replies:
            rpid = reply['rpid_str']
            uname = reply['member']['uname']
            message = reply['content']['message']
            like = reply['like']
            time_desc = reply['reply_control']['time_desc']
            location = reply['reply_control']['location']

            scraper.push_data({
                '评论ID': rpid,
                '用户名': uname,
                '评论内容': message,
                '点赞数': like,
                '发布时间': time_desc,
                'IP属地': location
            })

            print(f"评论ID: {rpid}")
            print(f"用户名: {uname}")
            print(f"评论内容: {message}")
            print(f"点赞数: {like}")
            print(f"发布时间: {time_desc}")
            print(f"IP属地: {location}")
            print("-" * 30)
    else:
        print(f"API返回错误，错误信息: {data['message']}")
else:
    print(f"请求失败，状态码：{response.status_code}")
